package com.coocaa.ops.admin.v1.auth.sys.biz.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.coocaa.ops.admin.v1.auth.sys.biz.ISysMenuBiz;
import com.coocaa.ops.admin.v1.auth.sys.mapper.ISysMenuMapper;
import com.coocaa.ops.common.core.base.BaseBizImpl;
import com.coocaa.ops.common.core.base.model.PageResultModel;
import com.coocaa.ops.common.core.base.mybatis.QueryTemplate;
import com.coocaa.ops.common.core.biz.auth.entity.SysMenuEntity;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * @author bijiahao
 * @date : 2019/3/11.
 * @description
 */
@Service
public class SysMenuBizImpl extends BaseBizImpl<ISysMenuMapper, SysMenuEntity> implements ISysMenuBiz {

    @Override
    public Integer getMenuIdByCategory(String[] categories) {
        SysMenuEntity menu = querySysMenuEntity(categories);
        return null != menu ? menu.getId() : null;
    }

    @Override
    public String getMenuNameByByCategory(String[] categories) {
        SysMenuEntity menu = querySysMenuEntity(categories);
        return null != menu ? menu.getMenuName() : "";
    }

    @Override
    public SysMenuEntity add(SysMenuEntity entity) {
        getMapper().insert(entity);
        return entity;
    }

    @Override
    public PageResultModel<SysMenuEntity> getCommonPageList(Map<String, Object> params, boolean isQueryRls) {
        params.put("orderByStr", "id DESC");
        return super.getCommonPageList(params, isQueryRls);
    }

    /**
     * 通过菜单标识查询菜单信息
     *
     * @param menuToken
     * @return
     */
    @Override
    public SysMenuEntity getMenuByToken(String menuToken) {
        QueryWrapper<SysMenuEntity> wrapper = new QueryWrapper();
        wrapper.eq("menu_token", menuToken);
        List<SysMenuEntity> menuList = getMapper().selectList(wrapper);
        return CollectionUtils.isNotEmpty(menuList) ? menuList.get(0) : null;
    }

    /*************** ===私有方法=== ********************* ===start=== ***************************************/

    /**
     * 通过elid查询菜单信息
     * @param categories
     * @return
     */
    private SysMenuEntity querySysMenuEntity(String[] categories) {
        QueryTemplate<SysMenuEntity> wrapper = new QueryTemplate<>();
        if (categories.length > 1) {
            wrapper.lambda().in(SysMenuEntity::getMenuToken, categories);
        } else {
            wrapper.lambda().eq(SysMenuEntity::getMenuToken, categories[0]);
        }
        List<SysMenuEntity> list = getMapper().selectList(wrapper);
        return CollectionUtils.isNotEmpty(list) ? list.get(0) : null;
    }

}
